# -*- coding: utf-8 -*-
# @Time    :2021/1/13 22:57
# @Author  :robot_zsj
# @File    :app_1.py
from flask import Flask, Blueprint, request, make_response, jsonify, render_template
from sqlalchemy import text
from application import db
from common.models.user import User
'''
post/index 列表
post/info 详情
post/set 添加
post/ops 操作
'''

index_page = Blueprint("index_page", __name__)


@index_page.route("/")
def index_page_index():
    return "imooc index page"


@index_page.route("/my")
def hello():
    return "hello, I Love Imooc"


@index_page.route("/get")
def get():
    # var_a = request.args.get("a", "i love imooc")
    req = request.values
    var_a = req['a'] if 'a' in req else ""
    return "request:%s,params:%s,var_a:%s" \
           % (request.method, request.args, var_a)


@index_page.route('/post', methods=["POST"])
def post():
    # var_a = request.form['a'] if 'a' in request.form else ""
    req = request.values
    var_a = req['a'] if 'a' in req else ""
    return "request:%s,params:%s,var_a:%s" \
           % (request.method, request.form, var_a)


@index_page.route("/upload", methods=["POST"])
def upload():
    f = request.files['file'] if 'file' in request.files else None
    return "request:%s,params:%s,f:%s" % \
           (request.method, request.files, f)


@index_page.route("/text")
def text_():
    return "text/html"


@index_page.route("/text_same")
def text_same():
    response = make_response("text/html", 200)
    return response


@index_page.route("/json")
def json():
    import json
    data = {"a": "b"}
    response = make_response(json.dumps(data))
    response.headers["Content-Type"] = "application/json"
    return response


@index_page.route("/json_same")
def json_same():
    data = {"c": "c"}
    response = make_response(jsonify(data))
    return response


# 默认查找当前目录下templates目录下的html文件
@index_page.route("/template")
def template():
    ## 传值
    name = "imooc1"
    context = {"name": name}
    context['user'] = {"nickname": "zsj", "qq": "xxx", "home_page": "http://www.baidu.com"}
    context['num_list'] = [1, 2, 3, 4, 5]

    # 传输数据库
    sql = text("select * from `user`")
    result = db.engine.execute(sql)



    result = User.query.all()
    context['result'] = result
    return render_template("index.html", **context)

@index_page.route("/extend_template")
def extend_template():
    return render_template("extend_template.html")

@index_page.route("/extend_template_other")
def extend_template_other():
    return render_template("extend_template_other.html")
